<?php
/**
 * @category ContentEngine
 * @package Goatherd\Content\Engine
 *
 * @author Copyright (c) 2012 Maik Penz <maik@phpkuh.de>
 * @version $Id: AbstractItem.php 87 2012-03-28 22:52:43Z maik@phpkuh.de $
 *
 * This file is part of Goatherd library.
 *
 * Goatherd library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * Goatherd library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Goatherd library. If not, see <http://www.gnu.org/licenses/>.
 */
namespace Goatherd\Content\Engine\Queue;

use Goatherd\Commons\Model\ModelTrait;

use Goatherd\Content\Engine\Source\ISource;

/**
 *
 * @category ContentEngine
 * @package Goatherd\Content\Engine\Reader
 * @subpackage Queue
 */
abstract class AbstractItem
implements IItem
{
    use ModelTrait;

    /**
     *
     * @var AbstractSource
     */
    protected $_source = null;

    /**
     *
     * @var bool
     */
    protected $_clean = true;

    /**
     *
     * @var mixed
     */
    protected $_identifier = null;

    /**
     *
     * @var \DateTime
     */
    protected $_schedule = null;

    /**
     * (non-PHPdoc)
     * @see Goatherd\Content\Engine\Queue.IItem::getSource()
     */
    public function getSource()
    {
        return $this->_source;
    }

    /**
     * (non-PHPdoc)
     * @see Goatherd\Content\Engine\Queue.IItem::setSource()
     */
    public function setSource(ISource $source)
    {
        if ($source === null) {
            throw new Exception('Source must not be null.');
        }
        if ($this->_source !== null) {
            throw new Exception('Source already set.');
        }
        $this->_source = $source;

        return $this;
    }

    /**
     * (non-PHPdoc)
     * @see Goatherd\Content\Engine\Queue.IItem::getIdentifier()
     */
    public function getIdentifier()
    {
        return $this->_identifier;
    }

    /**
     * (non-PHPdoc)
     * @see Goatherd\Content\Engine\Queue.IItem::setIdentifier()
     */
    public function setIdentifier($id)
    {
        if ($this->_identifier !== null) {
            throw new Exception('ID already set.');
        }
        $this->_identifier = $id;

        return $this;
    }

    /**
     * (non-PHPdoc)
     * @see Goatherd\Content\Engine\Queue.IItem::getResource()
     */
    public function getResource()
    {
        $rs = $this->getSource()->getResource($item);

        return $rs;
    }

    /**
     * (non-PHPdoc)
     * @see Goatherd\Content\Engine\Queue.IItem::isValid()
     */
    public function isValid()
    {
        return true;
    }

    /**
     * (non-PHPdoc)
     * @see Goatherd\Content\Engine\Queue.IItem::isClean()
     */
    public function isClean()
    {
        return $this->_clean;
    }

    /**
     * (non-PHPdoc)
     * @see Goatherd\Content\Engine\Queue.IItem::persist()
     */
    abstract public function persist();

    /**
     * (non-PHPdoc)
     * @see Goatherd\Content\Engine\Queue.IItem::getRelatedItems()
     */
    abstract public function getRelatedItems();
}